package question2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @FileName Server
 * @Description
 * @Author Sword
 * @date 2024-12-24
 **/
public class Server {
    public static void main(String[] args) throws IOException {
        int port = 12345;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("服务器已启动，正在监听端口 " + port);

        while (true) {
            Socket clientSocket = serverSocket.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println("收到客户端消息: " + inputLine);
                // 数据处理逻辑
                String response = "服务器已接收到数据: " + inputLine;
                out.println(response);
                break; // 或者根据需要继续读取
            }

            in.close();
            out.close();
            clientSocket.close();
        }
    }
}
